package gcb.metalsys.controller.cop;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import gcb.metalsys.enums.ProductEnum;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;
import gcb.metalsys.base.ResultUtil;
import gcb.metalsys.base.dto.ResultDTO;
import gcb.metalsys.service.cop.CopProductService;
import gcb.metalsys.entity.cop.CopProduct;

import java.util.List;

/**
 * @since 2019-12-30
 */
@RestController
@RequestMapping("/copProduct")
public class CopProductController {

    @Autowired
    private CopProductService copProductService;

    @PostMapping
    @ApiOperation(value = "插入")
    public ResultDTO insertOne(@RequestBody CopProduct copProduct) throws Exception {

        copProductService.save(copProduct);
        return ResultUtil.Success(copProduct);
    }

    @PostMapping("/import")
    @ApiOperation(value = "插入")
    public ResultDTO importExcel(MultipartFile file) {
        if (file.isEmpty()) {
            return ResultUtil.Error("500","文件为空!");
        }
        copProductService.readExcel(file);
        return ResultUtil.Success();
    }

    @PutMapping
    @ApiOperation(value = "按ID修改")
    public ResultDTO updateById(@RequestBody CopProduct copProduct) throws Exception {
        copProductService.updateById(copProduct);
        return ResultUtil.Success();
    }

    @DeleteMapping("/{id:\\d+}")
    @ApiOperation("按ID删除")
    public ResultDTO deleteById(@PathVariable Long id) throws Exception {
        copProductService.removeById((long) id);
        return ResultUtil.Success();
    }

    @GetMapping()
    @ApiOperation("查询、分页返回")
    public ResultDTO selectEntityPage(@RequestParam(value = "page", defaultValue = "0") int pageNum,
                                            @RequestParam(value = "limit", defaultValue = "8") int pageSize,
                                      String productName)throws Exception{


        QueryWrapper<CopProduct> copProductQueryWrapper = new QueryWrapper<>();
        if (productName != null && !productName.isEmpty()) {
            copProductQueryWrapper.like("product_name",productName);
        }

        IPage<CopProduct> page = copProductService.page(new Page<CopProduct>(pageNum, pageSize),copProductQueryWrapper);
        List<CopProduct> data = page.getRecords();

        for (CopProduct product : data) {
            for (ProductEnum value : ProductEnum.values()) {
                if (value.getCode().equals(product.getProductStatus())) {
                    product.setProductStatusName(value.getMessage());
                }
            }
        }
        int count = (int) page.getTotal();
        return ResultUtil.Success(data,count);
    }

    @GetMapping("/list")
    @ApiOperation("查询")
    public ResultDTO selectList()throws Exception{
        List<CopProduct> data = copProductService.list();
        int count = (int) data.size();
        return ResultUtil.Success(data,count);
    }

    @GetMapping("/{id:\\d+}")
    @ApiOperation("ID查询")
    public ResultDTO selectEntityPage(@PathVariable Long id)throws Exception{
        return ResultUtil.Success(copProductService.getById(id));
    }

}
